package com.example.sunmiv1.adapter;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.sunmiv1.R;
import com.example.sunmiv1.activity.RepayActivity;
import com.example.sunmiv1.activity.RepaymentRecordActivity;
import com.example.sunmiv1.entity.CreditUserListInfo;
import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.views.CircleImageView;
import com.example.sunmiv1.views.SlideLayout;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.content.ContentValues.TAG;

public class CreditUserAdapter extends BaseAdapter {

    private List<CreditUserListInfo.DataBean.ListBean> list;
    private ViewHolder viewHolder;
    private Set<SlideLayout> sets = new HashSet();
    private Intent intent;
    private CallBack callBack;
    private boolean type = false;
    private ACache aCache;

    public CreditUserAdapter(List<CreditUserListInfo.DataBean.ListBean> list) {
        this.list = list;
    }

    public void setType(boolean type) {
        this.type = type;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_credit_user, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        aCache = ACache.get(parent.getContext());
        CreditUserListInfo.DataBean.ListBean bean = list.get(position);
        if (bean.isCheck()) {
            viewHolder.itemCreditUserCheckImg.setImageResource(R.mipmap.repay_x);
        } else {
            viewHolder.itemCreditUserCheckImg.setImageResource(R.mipmap.repay_wx);
        }
        if (type) {
            viewHolder.itemCreditUserCheckLin.setVisibility(View.VISIBLE);
        } else {
//            Log.e(TAG, "getView: 111111111111111111111111111");
            viewHolder.itemCreditUserCheckLin.setVisibility(View.GONE);
        }
        final ImageView img = viewHolder.itemCreditUserCheckImg;
        final int index = position;
        viewHolder.itemCreditUserCheckLin.setOnClickListener(v -> {
            if (bean.isCheck()) {
                img.setImageResource(R.mipmap.repay_wx);
            } else {
                img.setImageResource(R.mipmap.repay_x);
            }
            bean.setCheck(!bean.isCheck());
        });
        if (TextUtils.isEmpty(bean.getAvatar()) || "-".equals(bean.getAvatar())) {
            viewHolder.itemCreditUserImg.setImageResource(R.mipmap.default_img);
        } else {
            RequestOptions options = new RequestOptions();
            options.override(300, 300);
            options.placeholder(R.mipmap.default_img);//佔位圖  .error(R.mipmap.error_food)//如果是錯誤的情況
            Glide.with(parent.getContext())
                    .load(list.get(position).getAvatar())
                    .apply(options)
                    .into(viewHolder.itemCreditUserImg);
        }
        viewHolder.itemCreditUserNick.setText(bean.getNickname());
        viewHolder.itemCreditUserName.setText(bean.getName());
        viewHolder.itemCreditUserPhone.setText(bean.getMobile_phone());
        viewHolder.itemCreditUserQuota.setText(bean.getQuota());
        viewHolder.itemCreditUserMoney.setText(bean.getArrearage_amount());
        viewHolder.itemCreditUserRepayMoney.setText(bean.getArrearage_amount());
        viewHolder.itemCreditUserMenu.setOnClickListener(v -> {
            SlideLayout slideLayout = (SlideLayout) v.getParent();
            slideLayout.closeMenu(); //解決刪除item後下一個item變成open狀態問題
            callBack.click(position, 2);
//            Toast.makeText(parent.getContext(), "點擊了第" + position + "個的刪除", Toast.LENGTH_SHORT).show();
        });
        viewHolder.itemCreditUserRepaymentRecord.setOnClickListener(v -> {
            intent = new Intent(parent.getContext(), RepaymentRecordActivity.class);
            intent.putExtra("id", bean.getCredit_id());
            parent.getContext().startActivity(intent);
        });
        if (!"3".equals(aCache.getAsString("userType"))) {
            viewHolder.itemCreditUserRepayment.setVisibility(View.GONE);
        }
        viewHolder.itemCreditUserRepayment.setOnClickListener(v -> {
//            if ("3".equals(aCache.getAsString("userType"))) {
                intent = new Intent(parent.getContext(), RepayActivity.class);
                intent.putExtra("id", bean.getCredit_id());
                intent.putExtra("vid", bean.getVid());
                intent.putExtra("creditMoney", bean.getArrearage_amount());
                parent.getContext().startActivity(intent);
//            }else{
//                Toast.makeText(parent.getContext(), "只有收銀員纔可以進行還款操作", Toast.LENGTH_SHORT).show();
//            }
        });
        viewHolder.itemCreditUserSetQuota.setOnClickListener(v -> {
            callBack.click(position, 1);
        });
        SlideLayout slideLayout = (SlideLayout) convertView;
        slideLayout.setOnStateChangeListener(new MyOnStateChangeListener());
        return convertView;
    }

    public interface CallBack {
        void click(int index, int type);//type:1設置金額 2:刪除
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    public SlideLayout slideLayout = null;

    class MyOnStateChangeListener implements SlideLayout.OnStateChangeListener {
        /**
         * 滑動後每次手勢擡起保證只有一個item是open狀態，加入sets集合中
         **/
        @Override
        public void onOpen(SlideLayout layout) {
            slideLayout = layout;
            if (sets.size() > 0) {
                for (SlideLayout s : sets) {
                    s.closeMenu();
                    sets.remove(s);
                }
            }
            sets.add(layout);
        }

        @Override
        public void onMove(SlideLayout layout) {
            if (slideLayout != null && slideLayout != layout) {
                slideLayout.closeMenu();
            }
        }

        @Override
        public void onClose(SlideLayout layout) {
            if (sets.size() > 0) {
                sets.remove(layout);
            }
            if (slideLayout == layout) {
                slideLayout = null;
            }
        }
    }

    static class ViewHolder {
        @BindView(R.id.itemCreditUser_checkImg)
        ImageView itemCreditUserCheckImg;
        @BindView(R.id.itemCreditUser_checkLin)
        LinearLayout itemCreditUserCheckLin;
        @BindView(R.id.itemCreditUser_img)
        CircleImageView itemCreditUserImg;
        @BindView(R.id.itemCreditUser_name)
        TextView itemCreditUserName;
        @BindView(R.id.itemCreditUser_nick)
        TextView itemCreditUserNick;
        @BindView(R.id.itemCreditUser_phone)
        TextView itemCreditUserPhone;
        @BindView(R.id.itemCreditUser_quota)
        TextView itemCreditUserQuota;
        @BindView(R.id.itemCreditUser_money)
        TextView itemCreditUserMoney;
        @BindView(R.id.itemCreditUser_repayMoney)
        TextView itemCreditUserRepayMoney;
        @BindView(R.id.itemCreditUser_setQuota)
        TextView itemCreditUserSetQuota;
        @BindView(R.id.itemCreditUser_repayment)
        TextView itemCreditUserRepayment;
        @BindView(R.id.itemCreditUser_repaymentRecord)
        TextView itemCreditUserRepaymentRecord;
        @BindView(R.id.itemCreditUser_main)
        LinearLayout itemCreditUserMain;
        @BindView(R.id.itemCreditUser_menu)
        LinearLayout itemCreditUserMenu;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
